
var TOTAL_PAGE = 5;
var PAGE_SIZE = 48;
var YOUTUBE_SEARCH ="http://gdata.youtube.com/feeds/api/videos?alt=json";
function initData(callback){
	type = new Array();

	type.push({name:'Most Viewed',totalPage:TOTAL_PAGE,curPage:1,url:'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?alt=json&lr='});

	//typeList.push({name:'Most Recent',totalPage:TOTAL_PAGE,curPage:1,url:'http://gdata.youtube.com/feeds/api/standardfeeds/most_recent?alt=json&lr='});
	type.push({name:'Top Rated',totalPage:TOTAL_PAGE,curPage:1,url:'http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?alt=json&lr='});
	
	type.push({name:'Most Discussed',totalPage:TOTAL_PAGE,curPage:1,url:'http://gdata.youtube.com/feeds/api/standardfeeds/most_discussed?alt=json&lr='});
	
	type.push({name:'Top Favorites',totalPage:TOTAL_PAGE,curPage:1,url:'http://gdata.youtube.com/feeds/api/standardfeeds/top_favorites?alt=json&lr='});
	
	type.push({name:'Featured',totalPage:TOTAL_PAGE,curPage:1,url:'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?alt=json&lr='});
	return type;
}

function loadDataList(type,page,callback){
	
	var s = (type.page - 1)*PAGE_SIZE + 2;
	var url = type.url + '&start-index='+s+'&max-results='+PAGE_SIZE; 
	showGTip(url);
	$.ajax({
		 url:url,
		 dataType:'json',
		 success:function(data){
		  	var dataList = parseJsonDataToList(data);
				var obj = {
				count:58,					//总记录数
				page:page,					//当前页数
				dataList:dataList,				//数据
			}
			obj.totalPage = parseInt((obj.count -1 )/PAGE_SIZE) + 1;
			showGTip(obj.totalPage+"测试"+callback);
			callback(obj);
		 },
		 error:function(e){
			// mShowLogToFile("ajax error:"+e);
			 callback(false);
		}
	})
}






//change youtube JSON data to display data;
function parseJsonDataToList(data){
	var dataList = new Array();
	var entryList = data.feed.entry;
	
	if(!entryList) return dataList;
	for(var i = 0;i<entryList.length;i++){
		var entry = entryList[i];
		
		var img = '',content='',times='',time='';
		var thumbnail = (entry.media$group.media$thumbnail);
		if(thumbnail){ img = thumbnail[0].url}
		if(entry.content) {content = entry.content.$t;}
		if(entry.yt$statistics){ times = entry.yt$statistics.viewCount}
		if(entry.media$group.yt$duration){ time = formatTime(entry.media$group.yt$duration.seconds);}
		var data = {
			id:i+1,
			title:entry.title.$t,
			name:entry.title.$t,
			image:img,
			author:entry.author[0].name.$t,
			url:entry.link[0].href.replace(/003d/g,"="),
			describe:content,
			time:time,

			times:times,
		}
		
		
		
		dataList.push(data);
	}
	
	return dataList;
}

function formatTime(time){
	var t = '';
	
	if(!isNaN(time)){
		time = parseInt(time);
		var h = parseInt(time/3600);
		if(h>0){
			t+=''+h+':';	
		}
		time = time - h*3600;
		var m = parseInt(time/60);
		time = time - m*60;
		
		if(m<10){m='0'+m;}
		t+=m+':';
		
		if(time<10){time='0'+time;}
		t+=time;
	}
	return t;
}